package com.ctshk.rpc.order.tour.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.order.tour.code.CollectionStatusCode;
import com.ctshk.rpc.order.tour.code.SignOutStatusCode;
import com.ctshk.rpc.order.tour.dto.SeriesOrderTouristVisaDetailDTO;
import com.ctshk.rpc.order.tour.dto.SeriesOrderTouristVisaListDTO;
import com.ctshk.rpc.order.tour.entity.SeriesOrderTouristVisa;
import com.ctshk.rpc.order.tour.mapper.SeriesOrderTouristVisaMapper;
import com.ctshk.rpc.order.tour.req.*;
import com.ctshk.rpc.order.tour.service.ISeriesOrderTouristVisaService;
import com.ctshk.rpc.system.dto.UserDTO;
import com.ctshk.rpc.system.service.ISysUserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 系列团游客签证记录 服务实现类
 * </p>
 *
 * @author 谭响
 * @since 2021-01-27
 */
@DubboService
public class SeriesOrderTouristVisaServiceImpl extends ServiceImpl<SeriesOrderTouristVisaMapper, SeriesOrderTouristVisa> implements ISeriesOrderTouristVisaService {

    @Autowired
    private SeriesOrderTouristVisaMapper seriesOrderTouristVisaMapper;

    @DubboReference
    private ISysUserService iSysUserService;

//    /**
//     * 添加
//     */
//    @Override
//    public Result add(SeriesOrderTouristVisaAddReq req) {
//        SeriesOrderTouristVisa saveObject = EntityUtil.copy(req, SeriesOrderTouristVisa.class);
//        save(saveObject);
//        return Result.success();
//    }

    /**
     * 保存批量
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result saveBatch(List<SeriesOrderTouristVisaSaveBatchReq> listReq) {
        List<SeriesOrderTouristVisa> saveObjectList = EntityUtil.copyList(listReq, SeriesOrderTouristVisa.class);
        saveBatch(saveObjectList);
        return Result.success();
    }

    /**
     * 保存或更新批量
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result saveOrUpdateBatch(List<SeriesOrderTouristVisaSaveBatchReq> listReq) {
        List<SeriesOrderTouristVisa> updateObjectList = EntityUtil.copyList(listReq, SeriesOrderTouristVisa.class);
        saveOrUpdateBatch(updateObjectList);
        return Result.success();
    }

    /**
     * 列表
     */
    @Override
    public PageResponse<SeriesOrderTouristVisaListDTO> list(SeriesOrderTouristVisaListReq req) {
        IPage<SeriesOrderTouristVisaListReq> pageParam = new Page<>(req.getPageNo(), req.getPageSize());
        IPage<SeriesOrderTouristVisaListDTO> pageResult = seriesOrderTouristVisaMapper.selectListBySelective(pageParam, req);
        for (SeriesOrderTouristVisaListDTO record : pageResult.getRecords()) {
            if(record.getSubmitUserId() != null) {
                UserDTO submitUser = iSysUserService.loadUserInfo(record.getSubmitUserId(), 1);
                if (submitUser != null) {
                    record.setSubmitUserName(submitUser.getCnSurname() + submitUser.getCnName());
                    record.setSubmitUserDept(submitUser.getDept());
                }
            }
            if(record.getMakeUpUserId() != null) {
                UserDTO makeUpUser = iSysUserService.loadUserInfo(record.getMakeUpUserId(), 1);
                if (makeUpUser != null) {
                    record.setMakeUpUserName(makeUpUser.getCnSurname() + makeUpUser.getCnName());
                    record.setMakeUpUserDept(makeUpUser.getDept());
                }
            }
        }
        PageResponse<SeriesOrderTouristVisaListDTO> response = new PageResponse<>(pageResult.getRecords(),
                pageResult.getCurrent() == 1,
                pageResult.getTotal() <= (pageResult.getCurrent() * pageResult.getSize()),
                pageResult.getTotal(), pageResult.getCurrent(),
                pageResult.getSize());
        return response;
    }

    /**
     * 提交批量
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result submitBatch(SeriesOrderTouristVisaSubmitBatchReq req) {
        List<SeriesOrderTouristVisa> updateObjectList = new ArrayList<>();
        for (Long touristId : req.getTouristIdList()) {
            QueryWrapper<SeriesOrderTouristVisa> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(SeriesOrderTouristVisa::getOrderId, req.getOrderId());
            queryWrapper.lambda().eq(SeriesOrderTouristVisa::getTouristId, touristId);
            SeriesOrderTouristVisa dbVisa = seriesOrderTouristVisaMapper.selectOne(queryWrapper);
            if (dbVisa == null || !Objects.equals(dbVisa.getCollectionStatus(), CollectionStatusCode.NOT.getCode())) {
                continue;
            }
            SeriesOrderTouristVisa updateObject = EntityUtil.copy(req, SeriesOrderTouristVisa.class);
            updateObject.setId(dbVisa.getId());
            updateObject.setSubmitUserId(req.getUserId());
            updateObject.setCollectionStatus(CollectionStatusCode.ALREADY.getCode());
            updateObject.setSignOutStatus(SignOutStatusCode.WAIT_SEND.getCode());
            updateObjectList.add(updateObject);
        }
        updateBatchById(updateObjectList);
        return Result.success();
    }

    /**
     * 送签批量
     *
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result sendBatch(SeriesOrderTouristVisaSendBatchReq req) {
        List<SeriesOrderTouristVisa> updateObjectList = new ArrayList<>();
        for (Long touristId : req.getTouristIdList()) {
            QueryWrapper<SeriesOrderTouristVisa> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(SeriesOrderTouristVisa::getOrderId, req.getOrderId());
            queryWrapper.lambda().eq(SeriesOrderTouristVisa::getTouristId, touristId);
            SeriesOrderTouristVisa dbVisa = seriesOrderTouristVisaMapper.selectOne(queryWrapper);
            if (dbVisa == null || !Objects.equals(dbVisa.getSignOutStatus(), SignOutStatusCode.WAIT_SEND.getCode()) ||
                    !Objects.equals(dbVisa.getCollectionStatus(), CollectionStatusCode.ALREADY.getCode())) {
                continue;
            }
            SeriesOrderTouristVisa updateObject = EntityUtil.copy(req, SeriesOrderTouristVisa.class);
            updateObject.setId(dbVisa.getId());
            updateObject.setSignOutStatus(SignOutStatusCode.WAIT_OUT.getCode());
            updateObjectList.add(updateObject);
        }
        updateBatchById(updateObjectList);
        return Result.success();
    }

    /**
     * 登记批量
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result enterBatch(SeriesOrderTouristVisaEnterBatchReq req) {
        List<SeriesOrderTouristVisa> updateObjectList = new ArrayList<>();
        for (Long touristId : req.getTouristIdList()) {
            QueryWrapper<SeriesOrderTouristVisa> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(SeriesOrderTouristVisa::getOrderId, req.getOrderId());
            queryWrapper.lambda().eq(SeriesOrderTouristVisa::getTouristId, touristId);
            SeriesOrderTouristVisa dbVisa = seriesOrderTouristVisaMapper.selectOne(queryWrapper);
            if (dbVisa == null || !Objects.equals(dbVisa.getSignOutStatus(), SignOutStatusCode.WAIT_SEND.getCode()) ||
                    !Objects.equals(dbVisa.getCollectionStatus(), CollectionStatusCode.ALREADY.getCode())) {
                continue;
            }
            SeriesOrderTouristVisa updateObject = EntityUtil.copy(req, SeriesOrderTouristVisa.class);
            updateObject.setId(dbVisa.getId());
            updateObject.setCollectionStatus(CollectionStatusCode.NOT_SUPPLEMENT.getCode());
            updateObjectList.add(updateObject);
        }
        updateBatchById(updateObjectList);
        return Result.success();
    }

    /**
     * 补充批量
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result supplementBatch(SeriesOrderTouristVisaSupplementBatchReq req) {
        List<SeriesOrderTouristVisa> updateObjectList = new ArrayList<>();
        for (Long touristId : req.getTouristIdList()) {
            QueryWrapper<SeriesOrderTouristVisa> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(SeriesOrderTouristVisa::getOrderId, req.getOrderId());
            queryWrapper.lambda().eq(SeriesOrderTouristVisa::getTouristId, touristId);
            SeriesOrderTouristVisa dbVisa = seriesOrderTouristVisaMapper.selectOne(queryWrapper);
            if (dbVisa == null || !Objects.equals(dbVisa.getSignOutStatus(), SignOutStatusCode.WAIT_SEND.getCode()) ||
                    !Objects.equals(dbVisa.getCollectionStatus(), CollectionStatusCode.NOT_SUPPLEMENT.getCode())) {
                continue;
            }
            SeriesOrderTouristVisa updateObject = EntityUtil.copy(req, SeriesOrderTouristVisa.class);
            updateObject.setId(dbVisa.getId());
            updateObject.setCollectionStatus(CollectionStatusCode.ALREADY.getCode());
            updateObjectList.add(updateObject);
        }
        updateBatchById(updateObjectList);
        return Result.success();
    }

    /**
     * 出签批量
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result outBatch(SeriesOrderTouristVisaOutBatchReq req) {
        List<SeriesOrderTouristVisa> updateObjectList = new ArrayList<>();
        for (Long touristId : req.getTouristIdList()) {
            QueryWrapper<SeriesOrderTouristVisa> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(SeriesOrderTouristVisa::getOrderId, req.getOrderId());
            queryWrapper.lambda().eq(SeriesOrderTouristVisa::getTouristId, touristId);
            SeriesOrderTouristVisa dbVisa = seriesOrderTouristVisaMapper.selectOne(queryWrapper);
            if (dbVisa == null || !Objects.equals(dbVisa.getSignOutStatus(), SignOutStatusCode.WAIT_OUT.getCode())) {
                continue;
            }
            SeriesOrderTouristVisa updateObject = EntityUtil.copy(req, SeriesOrderTouristVisa.class);
            updateObject.setId(dbVisa.getId());
            updateObjectList.add(updateObject);
        }
        updateBatchById(updateObjectList);
        return Result.success();
    }

    /**
     * 详情
     */
    @Override
    public Result<SeriesOrderTouristVisaDetailDTO> detail(SeriesOrderTouristVisaDetailReq req) {
        SeriesOrderTouristVisaDetailDTO dto = seriesOrderTouristVisaMapper.selectDetailBySelective(req);
        return Result.success(dto);
    }
}
